<?php
require_once 'inc/config.inc.php';
require 'lib/smarty/Smarty.class.php';
require 'inc/Team.class.php';
require 'inc/Stage.class.php';
require 'inc/City.class.php';

$team = new Team();
$stage = new Stage();
$city = new City();

function APICall($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    
    return $result;
}

// put this in your application
function convert2Unicode($tpl_output, &$smarty) {
    $tpl_output = iconv("ISO-8859-1", "UTF-8", $tpl_output);
    return $tpl_output;
}

$smarty = new Smarty();
$smarty->compile_check = true;
$smarty->template_dir = $smarty_settings['template_dir'];
$smarty->compile_dir = $smarty_settings['compile_dir'];
$smarty->config_dir   = $smarty_settings['config_dir'];
$smarty->caching = false;

if ($dev_env) {
    $smarty->debugging = true;
}

// get current language from url (first param)
$url_language = $_GET['lang'];

// get current page from SEO friendly URL
$current_page = $_GET['page'];

// get fixture selector
$selParam = $_GET['selectorParam'];

if (empty($current_page)) {
    $body_class = 'home';
} else {
    $body_class = $current_page;
}

// check if there is a cookie set
//$cookie_language = $_COOKIE['EUKO_Lang'];

// if no language is set in the URL or in the Cookie
// redirect user to default (English) version
if (empty($url_language)) {
    header('Location: '.$wwwroot.'/en/'); 
}

//if (empty($cookie_language)) {
//    if (in_array($url_language, $allowed_languages)) {
//        // set cookie with URL language
//        $Month = 2592000 + time();
//        // this adds 30 days to the current time
//        setcookie('EUKO_Lang', $url_language, $Month);
//    } else {
//        die('Sorry, but this language is not supported!');   
//    }
//}

if (in_array($url_language, $allowed_languages)) {
    $lang = $url_language;
} else {
    die('Sorry, but this language is not supported!');   
}

require_once 'inc/translations.'.$lang.'.inc.php';


//$lang = $_COOKIE['EUKO_Lang'];

// set the locale correctly
switch ($lang) {
    case 'de':
        setlocale (LC_ALL, 'de_DE', 'de_DE', 'de', 'ge');
    break;
    default:
        setlocale (LC_ALL, 'en_EN', 'en_EN', 'en', 'en');
}

$smarty->cache_dir    = 'cache/'.$lang;

// if page is fixtures and $selparam is empty,
if ($current_page == 'fixtures') {
    $teamsA = $team->retrieveAllByGroup('id', $lang, '1');
    $teamsB = $team->retrieveAllByGroup('id', $lang, '2');
    $teamsC = $team->retrieveAllByGroup('id', $lang, '3');
    $teamsD = $team->retrieveAllByGroup('id', $lang, '4');
    
    $stages = $stage->retrieveAllByLang('id', $lang, false);
    $cities = $city->retrieveAllByLang('id', $lang);
      
    /* Create an array of days in June for looping through on the calendar */

    $june = array();
    for($i=1;$i<=30;$i++) {
      $june_date = mktime(0,0,0,6,$i,2008);
      $june[$i] = array(
        'date' => date('Y-m-d',$june_date),
        'week_day' => date('w',$june_date), 
        'week_number' => date('W',$june_date),
        'fixtures' => in_array(date('Y-m-d',$june_date),$allowed_dates)
      );

    }
    $smarty->assign('june', $june);
    
    $smarty->assign('groupApickerHeading', $stages[0]->name);
    $smarty->assign('groupBpickerHeading', $stages[1]->name);
    $smarty->assign('groupCpickerHeading', $stages[2]->name);
    $smarty->assign('groupDpickerHeading', $stages[3]->name);
    
    $smarty->assign('teamsA', $teamsA);
    $smarty->assign('teamsB', $teamsB);
    $smarty->assign('teamsC', $teamsC);
    $smarty->assign('teamsD', $teamsD);
    
    $smarty->assign('stageBoxHeading', $selectorBoxHeadline[$lang]['stage']);
    $smarty->assign('cityBoxHeading', $selectorBoxHeadline[$lang]['city']);
    $smarty->assign('dateBoxHeading', $selectorBoxHeadline[$lang]['date']);
    
    $smarty->assign('stages', $stages);
    $smarty->assign('cities', $cities);
    $smarty->assign('dates', $allowed_dates);
    
    
    // verify selector paramater
    if (!empty($selParam)) {
        if (in_array($selParam, $allowed_teams) || in_array($selParam, $allowed_cc)) {
            $sel = 'team';
            if (strlen($selParam) == 2) {
                $result = APICall($wwwroot.'/lib/getFixtures.php?cc='.$selParam.'&lang='.$lang);
                
                // get team name based on that cc
                $team->retrieveByCC($selParam, $lang);
            } else {
                $result = APICall($wwwroot.'/lib/getFixtures.php?country_norm='.$selParam.'&lang='.$lang);
                
                // get team name based on that country slug
                $team->retrieveBySlug($selParam, $lang);
            }
            $smarty->assign('fixturePickerByParamHeading', strtr($selectorHeadline[$lang], array('%param%' => $team->name)));
        } elseif (in_array($selParam, $allowed_cities)) {
            $sel = 'city';
            $result = APICall($wwwroot.'/lib/getFixtures.php?city='.$selParam.'&lang='.$lang);
            $city->retrieveBySlug($selParam, $lang);
            $smarty->assign('fixturePickerByParamHeading', strtr($selectorHeadline[$lang], array('%param%' => $city->name)));
        } elseif (in_array($selParam, $allowed_stages)) {
            $sel = 'stage';
            $result = APICall($wwwroot.'/lib/getFixtures.php?stageId='.$selParam.'&lang='.$lang);
            $stage->retrieveBySlug($selParam, $lang);
            $smarty->assign('fixturePickerByParamHeading', strtr($selectorHeadline[$lang], array('%param%' => $stage->name)));
        } elseif (in_array($selParam, $allowed_dates)) {
            $sel = 'date';
            $result = APICall($wwwroot.'/lib/getFixtures.php?date='.$selParam.'&lang='.$lang);
            $smarty->assign('fixturePickerByParamHeading', strtr($selectorHeadline[$lang], array('%param%' => strftime("%A, %B %#d", strtotime($selParam)))));
        } else {
            die("no fixtures with that param: $selParam");   
        }
        
        //$smarty->assign('fixturesTable', $result);
    } else {
        // call getFixtures.php with params  
        $result = APICall($wwwroot.'/lib/getFixtures.php?getAllFixtures=1&lang='.$lang);
    }
    $smarty->assign('fixturesTable', $result);
    
    // put together the calendar download URL
    
    // http://technorati.com/events/http://alpha.eurokickoff.com/en/fixtures/
    $downloadCalBaseURL = "http://technorati.com/events/";
    $subscribeCalBaseURL = "webcal://technorati.com/events/";
    
    $downloadCalURL = $downloadCalBaseURL . "$wwwroot/$lang/fixtures/$selParam";
    $subscribeCalURL = $subscribeCalBaseURL . "$wwwroot/$lang/fixtures/$selParam";
    
    $smarty->assign('downloadCalBaseURL', $downloadCalBaseURL);
    $smarty->assign('subscribeCalBaseURL', $subscribeCalBaseURL);
    
    $smarty->assign('downloadCalURL', $downloadCalURL);
    $smarty->assign('subscribeCalURL', $subscribeCalURL);
    
    $smarty->assign('allFixturesHeadline', $allFixturesHeadline[$lang]);
}

// put together page title
$p_title = $page_title[$lang]['basic'];

if (empty($current_page)) {
	$p_title .= $page_title[$lang]['tagline'];
} else {
	$p_title .= $page_title[$lang]['separator'];
	$p_title .= $navigation[$lang][$current_page];
	
	if ($current_page == 'fixtures') {
		if ($smarty->_tpl_vars['fixturePickerByParamHeading']) {
			$p_title .= $page_title[$lang]['separator'];
			$p_title .= $smarty->_tpl_vars['fixturePickerByParamHeading'];
		}
	}
}



// assign global variables
$smarty->assign('lang', $lang);                          // Language Attribute
$smarty->assign('page', $current_page);


$smarty->assign('page_title', $p_title);       // Page Title
$smarty->assign('body_class', $body_class);
$smarty->assign('meta_description', $meta[$lang]['description']);
$smarty->assign('meta_keywords', $meta[$lang]['keywords']);
$smarty->assign('wwwroot', $wwwroot);


// navigation
$smarty->assign('nav_home', $navigation[$lang]['home']);       // Page Title
$smarty->assign('nav_fixtures', $navigation[$lang]['fixtures']);       // Page Title
$smarty->assign('nav_faq', $navigation[$lang]['faq']);       // Page Title
$smarty->assign('nav_about', $navigation[$lang]['about']);       // Page Title
$smarty->assign('nav_sponsorship', $navigation[$lang]['sponsorship']);       // Page Title
$smarty->assign('nav_contact', $navigation[$lang]['contact']);       // Page Title
$smarty->assign('nav_api', $navigation[$lang]['api']);       // Page Title
$smarty->assign('nav_promote', $navigation[$lang]['promote']);       // Page Title

// for fixture picker
$smarty->assign('sel', $sel);  
$smarty->assign('selParam', $selParam);                  // fixture selector Param

// fill main content
if (empty($current_page)) {
    $smarty->assign('main_content_slot1', $content[$lang]['home']);
} else {
    $smarty->assign('main_content_slot1', $content[$lang][$current_page]);
}

// fill footer content
$smarty->assign('footer_copyright', $footer[$lang]['copyright']);
$smarty->assign('footer_credits', $footer[$lang]['credits']);


// register the outputfilter
$smarty->register_outputfilter('convert2Unicode');

// render main template
$smarty->display('index.tpl'); 

// delete cookie
//setcookie ("EUKO_Lang", "", time() - 3600);


?>